unit uFunction;

interface

uses uDataModule, System.SysUtils, Vcl.Forms, System.IniFiles, IdHashMessageDigest;

  // IdHashMessageDigest = para poder gerar o md5   == IniFiles - manipular arquivos ini




function Conectar:Boolean;
function LerIni(InifileName, IniSessao, IniVar: String; VlDefault: String = ''): String;
procedure GravaIni(InifileName, IniSessao, IniVar, Valor: String);
function MD5(const texto:string):string;

implementation

procedure GravaIni(InifileName, IniSessao, IniVar, Valor: String);
var
  IniObj: TIniFile;
begin
        IniObj := TIniFile.Create(InifileName);
        try
                IniObj.WriteString(IniSessao, IniVar, Valor);
                IniObj.UpdateFile;
        finally
                FreeAndNil(IniObj);
        end;

end;

function LerIni(InifileName, IniSessao, IniVar, VlDefault: String): String;
var
  IniObj: TIniFile;
begin
        IniObj := TIniFile.Create(InifileName);
        try
                Result := IniObj.ReadString(IniSessao, IniVar, VlDefault);
        finally
                FreeAndNil(IniObj);
        end;
end;

function Conectar:Boolean;
var
  Path : String;
begin
  try
    with dtm_Principal.con_Mysql do
      begin
        Path := ExtractFilePath(Application.ExeName) + 'config.ini';
        Close;
        DriverName := 'MySQL';
        ConnectionName := 'MySQLConnection';
        GetDriverFunc := 'getSQLDriverMYSQL';
        LibraryName := 'dbxmys.dll';
        LoadParamsOnConnect := False;
        LoginPrompt := False;

        Params.Clear;
        DriverName                          := uFunction.LerIni(Path,'Conexao','DriverName','');
        Params.Values['HostName']           := uFunction.LerIni(Path,'Conexao','HostName','');
        Params.Values['DataBase']           := uFunction.LerIni(Path,'Conexao','DataBase','');
        Params.Values['User_Name']          := uFunction.LerIni(Path,'Conexao','User_name','');
        Params.Values['Password']           := uFunction.LerIni(Path,'Conexao','Password','');
        Params.Values['ServerCharSet']      := uFunction.LerIni(Path,'Conexao','ServerCharSet','');
        Params.Values['BlobSize']           := uFunction.LerIni(Path,'Conexao','BlobSize','');
        Params.Values['ErrorResourceFile']  := uFunction.LerIni(Path,'Conexao','ErrorResourceFile','');
        Params.Values['LocaleCode']         := uFunction.LerIni(Path,'Conexao','LocaleCode','');
        Params.Values['Compressed']         := uFunction.LerIni(Path,'Conexao','Compressed','');
        Params.Values['Encrypted']          := uFunction.LerIni(Path,'Conexao','Encrypted','');
        Params.Values['ConnectTimeout']     := uFunction.LerIni(Path,'Conexao','ConnectTimeout','');;

        VendorLib := 'libmysql.dll';
        Connected:=True;
        Result:= True;
      end;
  except
    Result:= False;
  end;
end;

function MD5(const texto:string):string;
var
  idmd5 : TIdHashMessageDigest5;
begin
  idmd5 := TIdHashMessageDigest5.Create;
  try
    result := idmd5.HashStringAsHex(texto);
  finally
    idmd5.Free;
  end;
end;





end.
